package com.example.changtest.ui

import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.view.View
import android.view.WindowInsetsController
import android.widget.Button
import android.widget.FrameLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import com.example.changtest.R
import com.example.changtest.util.LogUtil


class LayoutMeasureActivity : AppCompatActivity() {
//    WindowInsetsCompat.Type.ime() //键盘
//    WindowInsetsCompat.Type.statusBars() //状态栏
//    WindowInsetsCompat.Type.navigationBars() //导航栏
//    WindowInsetsCompat.Type.systemBars()  //状态栏、导航栏和标题栏

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_layout_measure)

        //设置状态栏和导航栏的底色，透明
        window.statusBarColor = Color.TRANSPARENT  //状态栏
        window.navigationBarColor = Color.TRANSPARENT
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            window.navigationBarDividerColor = Color.TRANSPARENT
        }
//
//        //过时方法
//        val frameLayout = findViewById<FrameLayout>(R.id.root_layout)
//        frameLayout.systemUiVisibility = (SYSTEM_UI_FLAG_LAYOUT_STABLE
//                or SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)

        //第二参数decorFitsSystemWindows表示是否沉浸，false 表示沉浸，true表示不沉浸
        WindowCompat.setDecorFitsSystemWindows(window, false)

        LogUtil.e("Build.VERSION.SDK_INT ${Build.VERSION.SDK_INT}") //29  30
        //设置状态栏,字体为黑色
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            window.insetsController?.setSystemBarsAppearance(
                WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS,
                WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS
            )
        } else {
            // API30以下
            window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
        }
//        ViewCompat.getWindowInsetsController(window.decorView)?.let { controller ->
//            controller.show(WindowInsetsCompat.Type.statusBars())
//            controller.isAppearanceLightStatusBars = true //状态栏,true字体黑色,false白色
//            //controller.isAppearanceLightNavigationBars = true
//            controller
//        }

        val button = findViewById<Button>(R.id.button)
        //当监听到WindowInsets发生变化时，我们调用systemWindowInsetTop即可获取到状态栏的高度，
        //然后对不需要延伸到状态栏区域的控件进行相应的偏移。
        ViewCompat.setOnApplyWindowInsetsListener(button) { view, insets ->
            val params = view.layoutParams as FrameLayout.LayoutParams
            params.topMargin = insets.systemWindowInsetTop
            insets
        }

    }


}